package Test1.Thermometry;

import Test1.HCNetSDK;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import java.io.UnsupportedEncodingException;

/**
 * Created by panting6 on 2021/6/25.
 */
public class SetROI {
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;

    /**
     * 测试设置ROI
     *
     * @param lUserID     用户登录句柄
     * @param iChannelNum 通道号
     */
    public void setROI(int lUserID, int iChannelNum) throws UnsupportedEncodingException {

        IntByReference pchannel = new IntByReference(iChannelNum);
        Pointer pChannelNum = pchannel.getPointer();

        HCNetSDK.NET_DVR_STD_CONFIG m_lpConfigPram6766 = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_THERMOMETRY_MODE m_lpTherMode6766 = new HCNetSDK.NET_DVR_THERMOMETRY_MODE();
        m_lpConfigPram6766.lpCondBuffer = pChannelNum;
        m_lpConfigPram6766.dwCondSize = 4;
        m_lpTherMode6766.dwSize=m_lpTherMode6766.size();
        m_lpTherMode6766.byMode = 0;
        m_lpTherMode6766.byThermometryROIEnabled = 0;
        m_lpConfigPram6766.lpInBuffer = m_lpTherMode6766.getPointer();
        m_lpConfigPram6766.dwInSize = m_lpTherMode6766.dwSize;
        m_lpTherMode6766.write();
        m_lpConfigPram6766.write();
        if (!hCNetSDK.NET_DVR_SetSTDConfig(lUserID, 6766, m_lpConfigPram6766)) {
            System.out.println("测温模式设置失败！！! 错误码：" + hCNetSDK.NET_DVR_GetLastError());
        } else {

            System.out.println("测温模式设置成功！" );
        }

        HCNetSDK.NET_DVR_STD_CONFIG lpConfigParam6765 = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_THERMOMETRY_MODE m_lpTherMode6765 = new HCNetSDK.NET_DVR_THERMOMETRY_MODE();

        lpConfigParam6765.lpCondBuffer = pChannelNum;
        lpConfigParam6765.dwCondSize = 4;
        lpConfigParam6765.lpInBuffer = null;
        lpConfigParam6765.dwInSize = 0;
        lpConfigParam6765.lpOutBuffer = m_lpTherMode6765.getPointer();
        lpConfigParam6765.dwOutSize = m_lpTherMode6765.size();
        lpConfigParam6765.write();
        m_lpTherMode6765.write();

        if(!hCNetSDK.NET_DVR_GetSTDConfig(lUserID,6765,lpConfigParam6765))
        {
            System.out.println( "获取测温模式参数失败，错误号：" + hCNetSDK.NET_DVR_GetLastError());
        }
        else{
            m_lpTherMode6765.read();
            System.out.println("测温模式：" + m_lpTherMode6765.byMode);
        }

        HCNetSDK.NET_DVR_STD_CONFIG m_lpConfigParam3625 = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_THERMOMETRY_COND m_lpThCond3625 = new HCNetSDK.NET_DVR_THERMOMETRY_COND();
        HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO m_lpThPresetInfp3625 = new HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO();
        m_lpThCond3625.dwSize = m_lpThCond3625.size();
        m_lpThCond3625.dwChannel = iChannelNum;
        m_lpThCond3625.wPresetNo = 1;
        m_lpThCond3625.write();
        m_lpConfigParam3625.lpCondBuffer = m_lpThCond3625.getPointer();
        m_lpConfigParam3625.dwCondSize = m_lpThCond3625.dwSize;
        HCNetSDK.BYTE_ARRAY m_szStatusBuf = new HCNetSDK.BYTE_ARRAY(4096 * 4);
        m_lpConfigParam3625.lpStatusBuffer = m_szStatusBuf.getPointer();
        m_lpConfigParam3625.dwStatusSize = 4096 * 4;
        m_lpThPresetInfp3625.dwSize = m_lpThPresetInfp3625.size();
        m_lpThPresetInfp3625.wPresetNo = 1;
        m_lpThPresetInfp3625.struPresetInfo[0] = new HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO_PARAM();
        m_lpThPresetInfp3625.struPresetInfo[0].byEnabled = 1;
        m_lpThPresetInfp3625.struPresetInfo[0].byRuleID = 1;
        m_lpThPresetInfp3625.struPresetInfo[0].wDistance = 1;
        m_lpThPresetInfp3625.struPresetInfo[0].fEmissivity = (float) 0.96;
        m_lpThPresetInfp3625.struPresetInfo[0].byDistanceUnit = 0;
        m_lpThPresetInfp3625.struPresetInfo[0].byReflectiveEnabled =0;
        // m_lpThPresetInfp3625.struPresetInfo[0].fReflectiveTemperature = 20;
        //m_lpThPresetInfp3625.struPresetInfo[0].szRuleName = new byte[]{1};
        m_lpThPresetInfp3625.struPresetInfo[0].byRuleCalibType = 1;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion = new HCNetSDK.NET_VCA_POLYGON();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.dwPointNum = 4;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[0] = new HCNetSDK.NET_VCA_POINT();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[0].fX = (float) 0.3;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[0].fY = (float) 0.3;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[0].write();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[1] = new HCNetSDK.NET_VCA_POINT();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[1].fX = (float) 0.3;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[1].fY = (float) 0.9;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[1].write();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[2] = new HCNetSDK.NET_VCA_POINT();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[2].fX = (float) 0.9;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[2].fY = (float) 0.9;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[2].write();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[3] = new HCNetSDK.NET_VCA_POINT();
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[3].fX = (float) 0.9;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[3].fY = (float) 0.3;
        m_lpThPresetInfp3625.struPresetInfo[0].struRegion.struPos[3].write();
        m_lpThPresetInfp3625.write();
        m_lpConfigParam3625.lpInBuffer = m_lpThPresetInfp3625.getPointer();
        m_lpConfigParam3625.dwInSize = m_lpThPresetInfp3625.dwSize;

        m_lpConfigParam3625.byDataType = 0;
        m_lpConfigParam3625.write();

        boolean bRet1 = hCNetSDK.NET_DVR_SetSTDConfig(lUserID, 3625, m_lpConfigParam3625);
        if (bRet1 == false) {
            System.out.println("预置点1参数设置失败！！! 错误码：" + hCNetSDK.NET_DVR_GetLastError());
        } else{
            System.out.println("预置点1参数设置成功！！! " );}

        HCNetSDK.NET_DVR_STD_CONFIG m_lpConfigPram6765 = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_THERMOMETRY_MODE m_lpTherMode = new HCNetSDK.NET_DVR_THERMOMETRY_MODE();
        m_lpConfigPram6765.lpCondBuffer = pChannelNum;
        m_lpConfigPram6765.dwCondSize = 4;
        m_lpConfigPram6765.lpOutBuffer = m_lpTherMode.getPointer();
        m_lpConfigPram6765.dwOutSize = m_lpTherMode.size();
        m_lpConfigPram6765.write();
        if (!hCNetSDK.NET_DVR_GetSTDConfig(lUserID, 6765, m_lpConfigPram6765)) {
            System.out.println("测温模式获取失败！！! 错误码：" + hCNetSDK.NET_DVR_GetLastError());
        } else {
            m_lpTherMode.read();
            System.out.println("测温模式获取成功！测温模式为：" + m_lpTherMode.byMode + " ROI使能：" + m_lpTherMode.byThermometryROIEnabled);
        }

        HCNetSDK.NET_DVR_STD_CONFIG m_lpConfigParam3624 = new HCNetSDK.NET_DVR_STD_CONFIG();
        HCNetSDK.NET_DVR_THERMOMETRY_COND m_lpThCond3624 = new HCNetSDK.NET_DVR_THERMOMETRY_COND();
        HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO m_lpThPresetInfp3624 = new HCNetSDK.NET_DVR_THERMOMETRY_PRESETINFO();
        m_lpThCond3624.dwSize = m_lpThCond3624.size();
        m_lpThCond3624.dwChannel = iChannelNum;
        m_lpThCond3624.wPresetNo = 1;
        m_lpThCond3624.write();
        m_lpConfigParam3624.lpCondBuffer = m_lpThCond3624.getPointer();
        m_lpConfigParam3624.dwCondSize = m_lpThCond3624.dwSize;
        m_lpConfigParam3624.lpOutBuffer = m_lpThPresetInfp3624.getPointer();
        m_lpConfigParam3624.dwOutSize = m_lpThPresetInfp3624.size();
        m_lpConfigParam3624.byDataType = 0;
        m_lpThPresetInfp3624.write();
        m_lpConfigParam3624.write();
        boolean bRet = hCNetSDK.NET_DVR_GetSTDConfig(lUserID, 3624, m_lpConfigParam3624);
        if (bRet == false) {
            System.out.println("预置点参数获取失败！！! 错误码：" + hCNetSDK.NET_DVR_GetLastError());
        } else {
            m_lpThPresetInfp3624.read();
            System.out.println("预置点参数获取成功!预置点：" + m_lpThPresetInfp3624.wPresetNo
                    + "，使能：" + m_lpThPresetInfp3624.struPresetInfo[0].byEnabled
                    + ",规则ID：" + m_lpThPresetInfp3624.struPresetInfo[0].byRuleID
                    + ",距离：" + m_lpThPresetInfp3624.struPresetInfo[0].wDistance
                    + ",发射率：" + m_lpThPresetInfp3624.struPresetInfo[0].fEmissivity
                    + ",距离单位：" + m_lpThPresetInfp3624.struPresetInfo[0].byDistanceUnit
                    + ",反射温度使能：" + m_lpThPresetInfp3624.struPresetInfo[0].byReflectiveEnabled
                    + ",反射温度：" + m_lpThPresetInfp3624.struPresetInfo[0].fReflectiveTemperature
                    + ",规则名称：" + new String(m_lpThPresetInfp3624.struPresetInfo[0].szRuleName, "GBK")
                    + ",规则标定类型：" + m_lpThPresetInfp3624.struPresetInfo[0].byRuleCalibType
                    + ",框测温坐标：（" + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[0].fX + "," + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[0].fY + ")"
                    + "（" + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[1].fX + "," + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[1].fY + ")"
                    + "（" + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[2].fX + "," + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[2].fY + ")"
                    + "（" + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[3].fX + "," + m_lpThPresetInfp3624.struPresetInfo[0].struRegion.struPos[3].fY + ")");
        }

    }
}
